import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_liquid_glass/liquid_glass.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/NetWorkImage/NetworkImageWidget.dart';
import 'package:sinosheets/components/product_favorite_btn.dart';
import 'package:sinosheets/main_config.dart';
import 'package:sinosheets/route/route_utils.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';
import 'package:sinosheets/utils/toast_util.dart';

///按钮事件的回调
typedef ProductAlertCallBack = void Function();

class FlashSaleProductAlert {
  /// 使用Get.back(result:T)回传数据
  static Future<T?> show<T>({
    required Map productInfo,
    String? tag,
    //突出颜色
    Color? highlightColor,
  }) {
    return Get.dialog(
      barrierDismissible: true, // 设置为 false 将禁用返回键和点击背景关闭
      Scaffold(
        backgroundColor: Colors.transparent,
        body: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(
                child: Container(
                  width: 280.r,
                  // height: 130.r,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: LiquidGlassContainer(
                    config: LiquidGlassConfig(
                      baseColor: ColorUtils.themeColor,
                      opacity: 0.1,
                      blurAmount: 20,
                      borderRadius: BorderRadius.circular(12.r),
                      enableSpecularHighlight: true,
                      refractionIntensity: 0.8,
                      gradient: LinearGradient(
                        colors: [
                          ColorUtils.themeColor.withValues(alpha: 0.1),
                          ColorUtils.themeColor.withValues(alpha: 0.1),
                        ],
                      ),
                      shadows: [
                        BoxShadow(
                          color: ColorUtils.themeColor.withValues(alpha: 0.2),
                          blurRadius: 20,
                          offset: const Offset(0, 10),
                        ),
                      ],
                    ),
                    padding: EdgeInsets.symmetric(horizontal: 15.r, vertical: 15.r),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Column(
                          children: [
                            SizedBox(height: 5.r),
                            Stack(
                              children: [
                                Center(
                                  child: ClipRRect(
                                    borderRadius: BorderRadius.circular(12.r),
                                    child: NetworkImageWidget(
                                      url: productInfo['img'],
                                      width: double.infinity,
                                      fit: BoxFit.fill,
                                    ),
                                  ),
                                ),
                                //右上角收藏
                                Positioned(
                                  right: 5.r,
                                  top: 5.r,
                                  child: ProductFavoriteBtn(productInfo: productInfo, isFlashSale: true),
                                ),
                              ],
                            ),
                            Padding(
                                padding: EdgeInsets.symmetric(horizontal: 10.r),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      productInfo['productName'],
                                      style: TextStyle(fontSize: 13.r, color: Colors.white),
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                    SizedBox(height: 8.r),
                                    Row(
                                      children: [
                                        Text('${productInfo['price']} ${productInfo['symbol']}', style: TextStyle(color: Color(0xFFFF7D00), fontSize: 14.r, fontWeight: FontWeight.bold)),
                                        SizedBox(width: 6.r),
                                        Text('${productInfo['crossedPrice']} ${productInfo['symbol']}', style: TextStyle(color: Color(0xFFBDBDBD), fontSize: 10.r, decoration: TextDecoration.lineThrough, decorationColor: Color(0xFFBDBDBD))),
                                      ],
                                    ),
                                    SizedBox(height: 10.r),

                                    ///复制码
                                    SizedBox(
                                      width: double.infinity,
                                      child: ElevatedButton(
                                        style: ElevatedButton.styleFrom(
                                          backgroundColor: Color(0xFFFF7D00),
                                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.r)),
                                          padding: EdgeInsets.symmetric(vertical: 0),
                                          minimumSize: Size(0, 36.r),
                                          elevation: 0,
                                        ),
                                        onPressed: () {
                                          Clipboard.setData(ClipboardData(text: '${productInfo['inId']}'));
                                          ToastUtil.showText('Copied');
                                        },
                                        child: Text('Copy No.${productInfo['inId']}', style: TextStyle(fontSize: 15.r, fontWeight: FontWeight.bold, color: Colors.white)),
                                      ),
                                    ),
                                    SizedBox(height: 5.r),
                                    SizedBox(
                                      width: double.infinity,
                                      child: ElevatedButton(
                                        style: ElevatedButton.styleFrom(
                                          backgroundColor: Color(0xFFFF7D00),
                                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.r)),
                                          padding: EdgeInsets.symmetric(vertical: 0),
                                          minimumSize: Size(0, 36.r),
                                          elevation: 0,
                                        ),
                                        onPressed: () {
                                          Get.back();
                                          RouteUtils.toWebView(url: "${AppPrefix.urlPrefix}/chooseSku?id=${productInfo['id']}");
                                        },
                                        child: Text('Buy Now', style: TextStyle(fontSize: 15.r, fontWeight: FontWeight.bold, color: Colors.white)),
                                      ),
                                    ),
                                  ],
                                ))
                          ],
                        ),
                      ],
                    ),
                  ),
                ),
              ),

              SizedBox(height: 10.r),
              //close button
              GestureDetector(
                onTap: () {
                  Get.back();
                },
                child: SizedBox(
                  width: 50.r,
                  height: 50.r,
                  child: LiquidGlassCard(
                    config: LiquidGlassConfig(
                      baseColor: ColorUtils.themeColor,
                      opacity: 0.1,
                      blurAmount: 20,
                      borderRadius: BorderRadius.circular(100.r),
                    ),
                    child: Center(
                      child: Icon(Icons.close, color: Colors.white, size: 24.r),
                    ),
                  ),
                ),
              ),
            ],
          );
        }),
      ),
      routeSettings: RouteSettings(name: tag != null ? 'flash_sale_product_alert_${tag}' : 'flash_sale_product_alert'),
    );
  }
}
